﻿/*****************************************************************
*
*	Netcare Agent Project
*		Copyright(C) 2010, Wicresoft EKA. All Rights Reserved.
*
*	Authors:
*		Miles Chen (milesch@wicresoft.com)
*		TingRong Shi (trshi@wicresoft.com)
*
*	Part of the product code depends on gSOAP under GPL license
*		Copyright(C) 2000-2010, Robert van Engelen, Genivia Inc. All Rights Reserved.
*
*****************************************************************/
#pragma once

// using win32
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <Objbase.h>
#include <Shlobj.h>
#pragma comment(lib, "Ole32.lib")
#pragma comment(lib, "Shell32.lib")

#include <direct.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#endif

#include <sstream>
#include <stdio.h>
#include <string.h>

#include "DateTime.h"

USING_STD;
USING_ALCATEL_SMS;


inline char* _UTF8(const wchar_t* value, int length=0)
{
#ifdef WIN32

	if(!value)
		return NULL;

	if(!length)
		length = wcslen(value);

	if(!length)
		return NULL;

	int nChars = ::WideCharToMultiByte(
		CP_UTF8,
		0,
		value,
		length,
		NULL,
		0,
		NULL,
		NULL);
	if (nChars == 0) return NULL;

	char* buffer = new char[nChars + 1];
	::WideCharToMultiByte(
		CP_UTF8,
		0,
		value,
		length,
		buffer,
		nChars,
		NULL,
		NULL); 
	buffer[nChars] = '\0';

	return buffer;
#else
	return NULL; // Linux doesn't use unicode char
#endif
}

inline wchar_t* _ANSI(const char* value, int length=0)
{
#ifdef WIN32
	if(!value)
		return NULL;

	if(!length)
		length = strlen(value);

	if(!length)
		return NULL;

	int nChars = ::MultiByteToWideChar(
		CP_ACP,
		0,
		value,
		length,
		NULL,
		0);
	if (nChars == 0) return NULL;

	wchar_t* buffer = new wchar_t[nChars + 1];
	::MultiByteToWideChar(
		CP_ACP,
		0,
		value,
		length,
		buffer,
		nChars); 
	buffer[nChars] = L'\0';

	return buffer;
#else
	return NULL; // Linux doesn't use unicode char
#endif
}

// for windows, it should return a path like
//  C:\ProgramData\ChinaTelecom\Netcare\Logs\xxx.log
//  or C:\Documents and Settings\All Users\Application Data\ChinaTelecom\Netcare\Logs\xxx.log
// while for linux, it's something like
//  /usr/share/china-telecom/netcare/logs/xxx.log
#ifdef WIN32
inline wchar_t* GetLoggerPath()
{
	wchar_t* p = new wchar_t[MAX_PATH];



	if(SUCCEEDED(SHGetFolderPath(NULL, 
		CSIDL_COMMON_APPDATA | CSIDL_FLAG_CREATE, 
		NULL, 
		0, 
		p))) 
	{
		// Make sure the directory exists!
		//	C:\ProgramData\ChinaTelecom\Netcare\Logs\
		//
		wcscat(p, L"\\ChinaTelecom\\");
		_wmkdir(p);
		wcscat(p, L"Netcare\\");
		_wmkdir(p);
		wcscat(p, L"Logs\\");
		_wmkdir(p);
	}
	else
	{
		wcscpy(p, L"c:\\");
	}


	CDateTime* now = CDateTime::Now();
	wostringstream oss;
	oss << now->ToDateStringW();
	//oss << L" 监控日志";
	oss << L".log";
	wcscat(p, oss.str().c_str());
	delete now;

	return p;
}
#else //Linux
inline char* GetLoggerPath()
{
#ifndef MAX_PATH
#define MAX_PATH 260
#endif

	char* p = new char[MAX_PATH];

	// Make sure the directory exists!
	//	/usr/share/china-telecom/netcare/logs/
	strcpy(p, "/usr/share/china-telecom/");
	mkdir(p, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
	strcat(p, "netcare/");
	mkdir(p, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
	strcat(p, "logs/");
	mkdir(p, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);


	CDateTime* now = CDateTime::Now();
	ostringstream oss;
	oss << now->ToDateString();
	//oss << " 监控日志";
	oss << ".log";
	strcat(p, oss.str().c_str());
	delete now;


	return p;
}
#endif


inline bool InitializeApp()
{
#ifdef WIN32
	HRESULT hRes = ::CoInitialize(NULL);
	if(FAILED(hRes))
		return false;
	else
		return true;
#else
	//TODO:
#endif
}

inline void UninitializeApp()
{
#ifdef WIN32
	CoUninitialize();
#else
	//TODO:
#endif
}